package com.sibbay.vta;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.sibbay.tools.Sharable;
import com.sibbay.tools.Tools;
import com.sibbay.vta.data.RootMenu;

public class MainActivity extends CommonActivity {

	private ListView mListFavorites;
	private FavListAdapter mFavListAdapter;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        final ListView menu = (ListView) findViewById(R.id.list);
        menu.setAdapter(new ArrListAdapter(this));
        menu.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> l, View v, int pos, long id) {
		        final Intent intent = new Intent();
		        intent.setClassName(getPackageName(), ServiceListActivity.class.getName());
		        intent.putExtra(ServiceListActivity.EXTRA_ARRAY_ID, new Sharable(((ArrListAdapter)l.getAdapter()).getArrayId(pos)));
		        intent.putExtra(ServiceListActivity.EXTRA_TITLE, ((TextView)v).getText());
		        startActivity(intent);
//		        overridePendingTransition(R.anim.push_left_out, R.anim.push_right_in);
			}
		});

        mListFavorites = (ListView) findViewById(R.id.list_favorites);
        mListFavorites.setEmptyView(findViewById(android.R.id.empty));
        mFavListAdapter = new FavListAdapter(this);
        mListFavorites.setAdapter(mFavListAdapter);
        mListFavorites.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> l, View v, int pos, long id) {
		        final Intent intent = new Intent();
		        intent.setClassName(getPackageName(), RouteListActivity.class.getName());
		        Favorites.get(MainActivity.this, mFavListAdapter.getKey(pos), intent);
		        startActivity(intent);
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		mFavListAdapter.refresh();
	}
	///
	
    private static final class ArrListAdapter extends BaseAdapter {
        
		private final Context mContext;

        public ArrListAdapter(final Context context) {
        	mContext = context;
        }

        @Override
        public int getCount() {
            return RootMenu.DATA.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final TextView tv;
            if (convertView == null) {
                tv = (TextView) LayoutInflater.from(mContext).inflate(
                        android.R.layout.simple_list_item_1, parent, false);
            } else {
                tv = (TextView) convertView;
            }
            tv.setText((String)RootMenu.DATA[position][0]);
            return tv;
        }
        
        ///
        
        Object[][] getArrayId(int position) {
        	return (Object[][]) RootMenu.DATA[position][1];
        }
    }

    private static final class FavListAdapter extends BaseAdapter {
        
		private final Context mContext;
		private String[][] mFavoritesArray;
		
        public FavListAdapter(final Context context) {
        	mContext = context;
        	mFavoritesArray = Favorites.getAsArray(mContext);
        }

		@Override
        public int getCount() {
            return mFavoritesArray.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewGroup vg;
            if (convertView == null) {
                vg = (ViewGroup) LayoutInflater.from(mContext).inflate(
                        R.layout.favorite_list_item, parent, false);
            } else {
                vg = (ViewGroup) convertView;
            }
            ((TextView)vg.findViewById(R.id.title)).setText(mFavoritesArray[position][Favorites.ARRAY_TITLE]);
            ((TextView)vg.findViewById(R.id.day)).setText(mFavoritesArray[position][Favorites.ARRAY_DAY]);
            return vg;
        }
        
        ///
        
        void refresh() {
        	mFavoritesArray = Favorites.getAsArray(mContext);
//        	notifyDataSetChanged();
        	notifyDataSetInvalidated();
        }

        String getKey(int position) {
			return Tools.getKey(mFavoritesArray[position][Favorites.ARRAY_URL]);
		}

    }
}
